import requests
import json
import pandas as pd
from bs4 import BeautifulSoup
import datetime
import time
import re
from lib.db import insertDb,getOne,updateDb

def spliderNews(query=''):
    url = 'https://opendata.baidu.com/data/inner?tn=reserved_all_res_tn&dspName=iphone&from_sf=1&dsp=iphone&resource_id=28565&alr=1&'+query
    headers={
        'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.79 Safari/537.36'
    }
    response = requests.get(url, headers=headers)
    text = response.text
    # json = json.dumps(text)
    # with open('./test.txt','w') as f:
    #     f.write(text)
    start = text.index('"result":') + len('"result":')
    count = 1
    end = 0
    data = []
    for i in range(start+1, len(text)):
        if text[i] == '{':
            count += 1
        elif text[i] == '}':
            count -= 1
        if count == 0 and end == 0:
            end = 1
            data = json.loads(text[start:i+1])
    # print(data['items'])
    return data['items']
    # return data['items']

def get_total():
    url = 'https://voice.baidu.com/act/newpneumonia/newpneumonia'
    headers={
        'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.79 Safari/537.36'
    }
    response = requests.get(url, headers=headers)
    text = response.text
    # json = json.dumps(text)
    start = text.index('"summaryDataIn":') + len('"summaryDataIn":')
    count = 1
    end = 0
    data = []
    for i in range(start+1, len(text)):
        if text[i] == '{':
            count += 1
        elif text[i] == '}':
            count -= 1
        if count == 0 and end == 0:
            end = 1
            data = json.loads(text[start:i+1])
    dataInsert = {
        'total':data['confirmed'],
        'input':data['overseasInput'],
        'zhiyu':data['cured'],
        'dead':data['died'],
        'yisi':data['unconfirmed'],
        'wuzz':data['asymptomatic'],
        'nowtotal':data['curConfirm'],
        'serious':data['icu'],
    }
    date = str(datetime.datetime.now().year)+'-'+str(datetime.datetime.now().month)+'-'+str(datetime.datetime.now().day)
    check = getOne('total','date',date)
    if(check == 0):
        insertDb(dataInsert,'total')
    else:
        updateDb(dataInsert,'total',check)

if __name__ == '__main__':
    # splider()
    # spliderNews()
    get_total()